package com.salton123.voice.player;

import android.media.MediaPlayer;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;

/**
 * User: newSalton@outlook.com
 * Date: 2017/10/11 16:41
 * ModifyTime: 16:41
 * Description:
 */
public class TimerAudioPlayerHelper {

    private static final String TAG = "TimerAudioPlayerHelper";
    private MediaPlayer mPlayer;
    private static TimerAudioPlayerHelper mInstance;
    private String mMusicPlayPath;

    public TimerAudioPlayerHelper setOnPreparedListener(MediaPlayer.OnPreparedListener listener) {
        mOnPreparedListener = listener;
        return this;
    }

    private MediaPlayer.OnPreparedListener mOnPreparedListener;

    public TimerAudioPlayerHelper setOnCompletionListener(MediaPlayer.OnCompletionListener listener) {
        mOnCompletionListener = listener;
        return this;
    }

    private MediaPlayer.OnCompletionListener mOnCompletionListener;

    private TimerAudioPlayerHelper() {
    }

    //Double Check
    public static TimerAudioPlayerHelper instance() {
        if (mInstance == null) {
            synchronized (TimerAudioPlayerHelper.class) {
                if (mInstance == null) {
                    mInstance = new TimerAudioPlayerHelper();
                }
            }
        }
        return mInstance;
    }

    public TimerAudioPlayerHelper path(String musicPlayPath) {
        this.mMusicPlayPath = musicPlayPath;
        return this;
    }

    public TimerAudioPlayerHelper pause() {
        mPlayer.pause();
        return this;
    }

    /**
     * 判断文件是否存在
     *
     * @param filePath
     * @return
     */
    public static boolean isFileExisted(String filePath) {
        if (TextUtils.isEmpty(filePath)) {
            return false;
        }
        try {
            File file = new File(filePath);
            return (file.exists() && file.length() > 0);
        } catch (Exception e) {
            return false;
        }
    }

    public TimerAudioPlayerHelper prepare() {
        if (TextUtils.isEmpty(this.mMusicPlayPath)) {
            Log.i(TAG, "[playMusic] invalid musicPlayPath = " + mMusicPlayPath);
            return this;
        }
        // if (!isFileExisted(mMusicPlayPath)) {
        //     MLog.info(TAG, "[playMusic] musicPlayFile does not exist = " + mMusicPlayPath);
        //     return this;
        // }
        if (mPlayer == null) {
            mPlayer = new MediaPlayer();
            if (mOnPreparedListener != null) {
                mPlayer.setOnPreparedListener(mOnPreparedListener);
            }
            if (mOnCompletionListener != null) {
                mPlayer.setOnCompletionListener(mOnCompletionListener);
            }
        }
        try {
            mPlayer.reset(); //把各项参数恢复到初始状态
            mPlayer.setDataSource(null, Uri.parse(mMusicPlayPath));
            mPlayer.prepare();  //进行缓冲
        } catch (Throwable e) {
            Log.i(TAG, "playMusic exception = " + e);
        }
        return this;
    }

    public TimerAudioPlayerHelper seekMusic(int seekPosition) {
        try {
            if (mPlayer != null && seekPosition >= 0) {
                mPlayer.seekTo(seekPosition);
            }
        } catch (Throwable e) {
            Log.i(TAG, "zhangge-musicstore getDuration exception = " + e);
        }
        return this;
    }

    public void restorePlayMusic(boolean isPlay) {
        if (mPlayer != null) {
            if (isPlay) {
                mPlayer.start();
            } else {
                if (mPlayer.isPlaying()) {
                    mPlayer.pause();
                }
            }
        }
    }

    public int getDuration() {
        try {
            if (mPlayer != null) {
                return mPlayer.getDuration();
            }
        } catch (Throwable e) {
            Log.i(TAG, "zhangge-musicstore getDuration exception = " + e);
        }
        return 0;
    }

    public int getCurDuration() {
        try {
            if (mPlayer != null) {
                return mPlayer.getCurrentPosition();
            }
        } catch (Throwable e) {

            Log.i(TAG, "zhangge-musicstore getDuration exception = " + e);
        }
        return 0;
    }


    public String getPlayingUrl() {
        return mMusicPlayPath;
    }


//    public TimerAudioPlayerHelper restore(boolean isPlayOrPause) {
//        if (mPlayer != null) {
//            if (isPlayOrPause) {
//                mPlayer.start();
//            } else {
//                mPlayer.pause();
//            }
//        }
//        return this;
//    }

    public void release() {
        if (mPlayer != null) {
            mPlayer.stop();
            mPlayer.release();
            mPlayer = null;
        }
        if (mOnPreparedListener != null) {
            mOnPreparedListener = null;
        }
        mMusicPlayPath = "";
    }

}
